// site.js

// AJAX support

var xmlHttp;

function getXmlHttpObject() {
    var xmlHttp = null;
   
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    } catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    if ( xmlHttp == null ) {
	    alert("Your browser does not support AJAX!");
	}
	
    return xmlHttp;
}

function post(func,url,params) {
	xmlHttp = getXmlHttpObject();
	
	if (xmlHttp == null) {
		return;
	}
	
	xmlHttp.onreadystatechange = func;
	xmlHttp.open("POST",url,true);
	if ( params != null ) {
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
	}
	xmlHttp.send(params);
}

// Vocab

function hit(first,second) {
	post(showQuestion,"genquestion.php","first=" + first + "&second=" + second);
}

function showQuestion() {
	
	if (xmlHttp.readyState == 4) {
		document.getElementById("status").innerHTML="";
		document.getElementById("question").innerHTML=xmlHttp.responseText;
	}
}

function list() {
	post(showList,"genlist.php",null);
}

function showList() {

	if (xmlHttp.readyState == 4) {
		document.getElementById("list").innerHTML=xmlHttp.responseText + "<br><br><br>";
	}
}

function check(i) {

	ans = document.quizform.answer.value;
	if ( i == ans ) {
		document.getElementById("status").style.color="green";
		document.getElementById("status").innerHTML="Correct";	
	} else {
		document.getElementById("status").style.color="red";
		document.getElementById("status").innerHTML="Wrong";	
	}
}

// Memory Game

function showGame() {

	var top = 110;
	var id = 0;
	for ( r = 0; r < 4 ; r++ ) {
		var left = 10;
		for ( c = 0; c < 4; c++ ) {
			style = "left:" + left + "px;top:" + top + 
					"px;position:absolute;width:100px;height:100px;border:1px solid #000;";
			topStyle = style + "background-color: #FDD017;z-index: 1";
			bottomStyle = style + "background-color: #FFF";
			document.write("<div class=\"cell\" id=\"top_" + id + "\" style=\"" + topStyle + "\" onclick=\"showBottom('" + id + "')\"></div>");
			document.write("<div class=\"cell\" id=\"bottom_" + id + "\" style=\"" + bottomStyle + "\"></div>");
			left += 101;
			id++;
		}
		top += 101;
	}
}

function getMatch(id) {

	var matches = document.gamef.matches.value;
	var pairs = matches.split(";");
	for( i = 0; i < pairs.length; i++ ) {
		var pair = pairs[i].split(",");		
		if ( pair[0] == id ) {
			return pair[1];
		} else if ( pair[1] == id ) {
			return pair[0];
		}
	}
	return "";
}

function setMatch(idx1,idx2) {

	if ( document.gamef.matches.value != "" ) {
		document.gamef.matches.value += ";";
	}
	document.gamef.matches.value += idx1 + "," + idx2;
}

function hideTop(id,vis) {
	setVisibility("top_" + id,vis);
}

function showBottom(id) {

	hideTop(id,"hidden");
 		
	if ( document.gamef.pick.value != "" &&
		 document.gamef.pick.value != getMatch(id) ) {
		setTimeout("hideTop('" + document.gamef.pick.value + "','visible')",1000);
		setTimeout("hideTop('" + id + "','visible')",1000);
	} 

	if ( document.gamef.pick.value == "" ) {
		document.gamef.pick.value = id;
	} else {
		document.gamef.pick.value = "";
	}
}

function fillGame() {
	post(fillCells,"memory.php",null);	
}

function reset() {
	
	document.gamef.pick.value = "";
	document.gamef.matches.value = "";
	for ( i = 0; i < 16; i++ ) {
		hideTop(i,"visible");
	}
}

function fillCell(cells,val) {
	
	var idx = Math.floor(Math.random()*16);
	while ( cells[idx] != null ) {
		idx = Math.floor(Math.random()*16);
	}
	cells[idx] = val;
	document.getElementById('bottom_' + idx).innerHTML = val;
	return idx;
}

function fillCells() {
	
	if (xmlHttp.readyState == 4) {
		reset();
		var pairs = xmlHttp.responseText.split(";");
		var cells = [];
		for ( i = 0; i < pairs.length; i++ ) {
			var pair = pairs[i].split(",");
			var eIdx = fillCell(cells,pair[0]);
			var kIdx = fillCell(cells,pair[1]);
			setMatch(eIdx,kIdx);
		}
	}
}

// Gallery Game

function stopOrStart() {

	document.gamef.targetShot.value = "no";
	document.gamef.correctTarget.value = "";
	
	if ( document.gamef.gameb.value == "Start Game" ) {
		document.gamef.gameb.value="Stop Game";
		document.gamef.lifeCt.value = "5";
		for (i = 1; i <= 5; i++) {
			setVisibility("lives" + i,"visible");
		}
		getTargets();
	} else {
		document.gamef.gameb.value="Start Game";
		document.gamef.lifeCt.value = "0";
		for (i = 1; i <= 5; i++) {
			setVisibility("lives" + i,"hidden");
		}
		hideTargets();
	}
}

function getTargets() {
	var type = [ "english", "korean" ];
	var first = Math.floor(Math.random()*2);
	var second = (first==0)?1:0;
	post(showTargets,"gallery.php","first=" + type[first] + "&second=" + type[second]);
}

function showWord(value) {
	var targetWord = document.getElementById('targetWord');
	targetWord.innerHTML = value;
	setVisibility("targetWord","visible");	
}

function decreaseLife() {
	var lifeCt = parseInt(document.gamef.lifeCt.value);
	if ( lifeCt > 0 ) {
		setVisibility("lives" + lifeCt,"hidden");
	}
	lifeCt--;
	if ( lifeCt == 0 ) {
		document.gamef.gameb.value="Start Game";
	}
	document.gamef.lifeCt.value = "" + lifeCt;
}

function hideTargets() {
	setVisibility("targetWord", "hidden");
	for (i = 1; i <= 4; i++) {
		setVisibility("target" + i,"hidden");
	}
}

function resetTargets() {
	hideTargets();
	if ( document.gamef.targetShot.value == "no" ) {
		decreaseLife();
	}
	if ( parseInt(document.gamef.lifeCt.value) > 0 ) {
		getTargets();
	}
}

function showTarget(id,value) {
	var target = document.getElementById(id);
	target.innerHTML = value;
	var left = Math.round(300*Math.random());
	target.style.marginLeft = left + "px";
	setVisibility(id,"visible");
}

function showTargets() {
	if (xmlHttp.readyState == 4) {
		document.gamef.targetShot.value = "no";
		var values = xmlHttp.responseText.split(";");
		showWord(values[0]);
		var options = values[1].split(",");
		for ( i = 0; i < 4; i++ ) {
			showTarget("target" + (i+1),options[i]);
		}
		document.gamef.correctTarget.value = values[2];
		setTimeout("resetTargets()",5000);
	}
}

function shoot(target) {
	document.gamef.targetShot.value = "yes";
	if ( target.innerHTML != document.gamef.correctTarget.value ) {
		decreaseLife();
		playSound("wrong");
	} else {
		playSound("right");
	}
	setTimeout("setVisibility('" + target.id + "','hidden')",500);
}

// Verbs

function conjugate() {
	var stem = document.verbf.stem.value;
	if ( stem == "" ) {
		alert("You must enter a dictionary form stem.");
		return;
	}
	var tense = "present";
	for ( i = 0; i < document.verbf.tense.length; i++ ) {
		if ( document.verbf.tense[i].checked ) {
			tense = document.verbf.tense[i].value;
		}
	}
	var politeness = "standard"
	for ( i = 0; i < document.verbf.politeness.length; i++ ) {
		if ( document.verbf.politeness[i].checked ) {
			politeness = document.verbf.politeness[i].value;
		}
	}
	var params = "stem=" + stem + "&tense=" + tense + "&politeness=" + politeness;
	post(showConjugation,"genverb.php",params);
}

function showConjugation() {
	if (xmlHttp.readyState == 4) {
		var results = xmlHttp.responseText;
		document.getElementById("cresults").innerHTML = results;
	}
}

// Other

function playSound(id) {
  var sound=document.getElementById(id);
  sound.Play();
}

function setVisibility(id,vis) {
	document.getElementById(id).style.visibility = vis;
}

function change_frame(url) {
	parent.contentframe.location = url;
}

function popup(url) {
	newwindow=window.open(url,'popup',
	   'height=400,width=350,resizable=true,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no');
	if (window.focus) {newwindow.focus();}
	return false;
}

function iload(id, url) {
	document.getElementById(id).src = url;
}

function resizeIframe(id) {
    var height = document.documentElement.clientHeight;
    height -= document.getElementById(id).offsetTop;
    
    // not sure how to get this dynamically
    height -= 40; /* whatever you set your body bottom margin/padding to be */
    
    document.getElementById(id).style.height = height +"px";
};